<?php

namespace Sisesporte\Controller;

use SisesporteAdmin\Form\AlunoForm;
use Zend\View\Model\ViewModel;
use SisesporteAdmin\Controller\CrudController;
use Sisesporte\Utils\Cep;

class AlunoController extends CrudController {

    public function __construct() {
        $this->entity = "Sisesporte\Entity\Aluno";
        $this->form = "SisesporteAdmin\Form\AlunoForm";
        $this->service = "Sisesporte\Service\AlunoService";
        $this->controller = "aluno-new";
        $this->route = "sisesporte-admin";
    }

    protected $msg;
    
    public function indexAction() {
        $form = new AlunoForm(NULL, $this->getEm());

        $request = $this->getRequest();

        if ($request->isPost()) {
            $post = array_merge(
                    $this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()
            );
            
            if (!empty($post['noImagem']['name'])) {
                $filter = new \Zend\Filter\File\RenameUpload($_SERVER['DOCUMENT_ROOT'] . "/upload/foto/");
                $filter->setUseUploadName(TRUE);
                $filter->filter($post['noImagem']);
            }

            $service = $this->getServiceLocator()->get($this->service);
            $entity = $service->insert($post);
            if ($entity) {
                $this->msg = TRUE;
                return $this->redirect()->toRoute('sisesporte-site', array('controller' => 'aluno-new', 'action' => 'view', 'id' => $entity));
            } else {
                $this->msg = "Erro ao Aluno Cadastrar";
            }
        }

        return new ViewModel(array('form' => $form));
    }

    public function viewAction() {

        $form = new AlunoForm(NULL, $this->getEm());
        $request = $this->getRequest();

        $repository = $this->getEm()->getRepository("Sisesporte\Entity\Pessoa");
        $entity = $repository->find($this->params()->fromRoute('id', 0));
        if ($this->params()->fromRoute('id', 0)) {
            $form->setDefaultsFromEntity($entity);
        }
        $this->msg = TRUE;
        if ($request->isPost()) {
            $form->setData($request->getPost());
            $service = $this->getServiceLocator()->get($this->service);
            $service->update($this->params()->fromRoute('id', 0), $request->getPost()->toArray());
        }
        return new ViewModel(array('form' => $form, 'entity' => $entity->getSqPessoa(), 'msg' => $this->msg));
    }

    public function getCidadeByUfAction() {
        if ($this->getRequest()->isPost()) {
            $param = $this->getRequest()->getPost();
            $service = $this->getServiceLocator()->get("Sisesporte\Service\CidadeService");
            $data = $service->getCidadeByUf($param['sqUf']);
            $viewModel = new ViewModel(array('data' => $data));
            return $viewModel->setTerminal(TRUE);
        }
    }

    public function buscarCepAction() {
        if ($this->getRequest()->isPost()) {

            $param = $this->getRequest()->getPost();
            $cep = new Cep();
            $viewModel = new ViewModel(array('data' => $cep->buscarCep(str_replace("-", "", $param['nuCep']))));
            $viewModel->setTerminal(TRUE);
            return $viewModel;
        }
    }

}
